package com.winit.openapi.oauth.util;

import java.util.Random;
import java.util.UUID;

import org.apache.oltu.oauth2.as.issuer.MD5Generator;
import org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.util.Base64Utils;

/**
 * oAuth2.0安全类，负责生成临时CODE，clientID，clientSecret等
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * minghao.deng 	1.0  		2017年6月22日 	Created
 *
 * </pre>
 * @since 1.
 */
public abstract class OAuth2SecurityUtil {

    public static String getRandomCode() {
        SecureRandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
        randomNumberGenerator.setDefaultNextBytesSize(4);
        // randomNumberGenerator.setSeed((username).getBytes());
        String hex = randomNumberGenerator.nextBytes().toHex();
        return hex;
    }

    /**
     * 生成指定位数的随机数字验证码
     * 
     * @return
     */
    public static String generateFixLenthRandomString(int length) {

        Random rm = new Random();

        // 获得随机数
        double pross = (1 + rm.nextDouble()) * Math.pow(10, length);

        // 将获得的获得随机数转化为字符串
        String fixLenthString = String.valueOf(pross);

        // 返回固定的长度的随机数
        return fixLenthString.substring(1, length + 1);
    }

    /**
     * 使用万邑联账号登录时系统自动生成的开发者账号
     * 
     * @param winitUserName
     * @return
     */
    public static String generateDeveloperAccount(String winitUserName) {
        String randomCode = getRandomCode();

        return winitUserName + "_" + randomCode;

    }

    /**
     * 使用万邑联账号登录时系统自动生成的开发者账号密码
     * 
     * @param winitUserName
     * @return
     */
    public static String generateDeveloperAccountPassword(String winitUserName) {

        String password = Base64Utils.encodeToString(winitUserName.getBytes());
        if (password.length() > 120) {
            password = password.substring(0, 120 - 1);
        }

        return password;

    }

    /**
     * 生成APP的KEY
     * 
     * @return
     */
    public static String generateClientKey() {
        String clientKey = Base64Utils.encodeToString(UUID.randomUUID().toString().getBytes()).toUpperCase();
        if (clientKey.length() > 120) {
            clientKey = clientKey.substring(0, 120 - 1);
        }

        return clientKey;

    }

    /**
     * 生成client的密匙
     * 
     * @return
     */
    public static String generateClientSecret() {
        StringBuffer b = new StringBuffer(UUID.randomUUID().toString());
        b.append(System.nanoTime());
        String scret = Base64Utils.encodeToString(b.toString().getBytes()).toUpperCase();
        if (scret.length() > 120) {
            scret = scret.substring(0, 120 - 1);
        }

        return scret;

    }

    /**
     * 生成临时授权CODE
     * 
     * @return
     * @throws OAuthSystemException
     */
    public static String generateAuthCode() throws OAuthSystemException {
        OAuthIssuerImpl oAuthIssuerImpl = new OAuthIssuerImpl(new MD5Generator());

        return oAuthIssuerImpl.authorizationCode().toUpperCase();

    }

    /**
     * 生成最终access_token
     * 
     * @return
     * @throws OAuthSystemException
     */
    public static String generateAcceccToken() throws OAuthSystemException {
        OAuthIssuerImpl authIssuerImpl = new OAuthIssuerImpl(new MD5Generator());
        String accessToken = authIssuerImpl.accessToken();

        return accessToken.toUpperCase();

    }

    public static String md5(String string, String salt) {
        String md5 = new Md5Hash(string, salt).toString();
        return md5;
    }

    public static String md5(String string) {
        String md5 = new Md5Hash(string, null).toString();
        return md5;
    }

    public static String sha256(Object source, String salt) {
        return new Sha256Hash(source, salt).toString();
    }

    public static String sha256(Object source) {
        return new Sha256Hash(source, null).toString();
    }

    public static String md5AndSha256(String source) {
        return sha256(md5(source));
    }

    public static String md5AndSha256(String source, String salt) {
        return sha256(md5(source, salt), salt);
    }

    public static void main(String[] args) throws InterruptedException {
        // for (int i = 0; i < 100; i++) {
        // System.err.println(getFixLenthRandomString(6));
        //
        // }
        // System.out.println(MessageFormat.format("该域名{0}被访问了 {1} 次.",
        // "www.winit.com.cn" , 10));
        for (int i = 0; i < 100; i++) {
            Double d = Math.random() * 5;
            System.err.println(d.intValue());
        }

    }

}
